package com.bfly.mall.product.controller;

import com.bfly.common.ResponseUtil;
import com.bfly.common.page.Pager;
import com.bfly.common.util.DataConvertUtils;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.context.PagerThreadLocal;
import com.bfly.core.security.ActionModel;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.mall.product.entity.ProductComment;
import com.bfly.mall.product.dto.ProductCommentFormDTO;
import com.bfly.mall.product.dto.ProductCommentListDTO;
import com.bfly.mall.product.enums.ProductCommentStatusEnum;
import com.bfly.mall.product.service.IProductCommentService;
import com.bfly.mall.order.entity.OrderSku;
import com.bfly.mall.order.dto.OrderSkuNamesDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品评论Controller
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/10/4 9:49
 */
@RestController
@RequestMapping(value = "/manage/product/comment")
public class ProductCommentController extends BaseController {

    @Autowired
    private IProductCommentService commentService;

    /**
     * 所有产品评论
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/10/4 9:49
     */
    @GetMapping(value = "/list")
    public void listComment(HttpServletResponse response, HttpServletRequest request) {
        PagerThreadLocal.set(request);
        Map<String, Object> exact = new HashMap<>(1);
        Map<String, String> unExact = new HashMap<>(3);
        String nickName = request.getParameter("nickName");
        String orderNo = request.getParameter("orderNo");
        String content = request.getParameter("content");
        String status = request.getParameter("status");
        if (StringUtils.isNotBlank(nickName)) {
            unExact.put("nick_name", nickName);
        }
        if (StringUtils.isNotBlank(orderNo)) {
            unExact.put("order_no", orderNo);
        }
        if (StringUtils.isNotBlank(content)) {
            unExact.put("content", content);
        }
        if (StringUtils.isNotBlank(status)) {
            exact.put("status", DataConvertUtils.convertToInteger(status));
        }
        // 只查询用户的评论
        exact.put("is_poster", true);
        Pager<ProductComment> pager = commentService.getPage(exact, unExact, null);
        Pager<ProductCommentListDTO> dtoPager = new Pager<>(pager.getPageNo(), pager.getPageSize(), pager.getTotalCount());

        if (pager.getData() != null) {
            List<ProductCommentListDTO> list = new ArrayList<>();
            for (ProductComment comment : pager.getData()) {
                ProductCommentListDTO dto = new ProductCommentListDTO();
                List<OrderSkuNamesDTO> skuNamesDTOS=new ArrayList<>();
                for(OrderSku sku:comment.getSkus()){
                    OrderSkuNamesDTO skuNamesDTO=new OrderSkuNamesDTO();
                    BeanUtils.copyProperties(sku,skuNamesDTO);
                    skuNamesDTOS.add(skuNamesDTO);
                }
                dto.setSkuNames(skuNamesDTOS);
                BeanUtils.copyProperties(comment, dto);
                list.add(dto);
            }
            dtoPager.setData(list);
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(dtoPager));
    }

    /**
     * 回复评论
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/10/3 21:49
     */
    @PostMapping(value = "/reply")
    @ActionModel(value = "回复评论")
    public void replyComment(HttpServletResponse response, @Valid @RequestBody ProductCommentFormDTO formDTO, BindingResult result) {
        validData(result);
        commentService.reply(getAdmin(),formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 审核评论
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/10/3 21:56
     */
    @GetMapping(value = "/verify")
    @ActionModel(value = "审核评论")
    public void editCommentStatus(HttpServletResponse response, @RequestParam("commentId") int commentId, @RequestParam("status") int status) {
        commentService.editCommentStatus(commentId, ProductCommentStatusEnum.getStatus(status));
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 删除评论
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/10/3 21:56
     */
    @PostMapping(value = "/delete")
    @ActionModel(value = "删除评论")
    public void delComment(HttpServletResponse response, @RequestBody Integer... ids) {
        commentService.remove(ids);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }
}